package com.qyhl.webtv.module_live.teletext.program;

import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.live.ProgramBean;
import com.qyhl.webtv.commonlib.entity.live.ProgramDetailBean;
import com.qyhl.webtv.module_live.common.LiveUrl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by qyhl on 2018/1/4.
 */

public class LiveNormalModel implements LiveNormalContract.LiveNormalModel {
    private LiveNormalPresenter mPresenter;

    public LiveNormalModel(LiveNormalPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    /**
     * 获取节目列表
     *
     * @param proId
     */
    @Override
    public void getList(String proId) {


        EasyHttp.get(LiveUrl.GET_PROGRAM_LIST)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("proId", proId)
                .execute(new SimpleCallBack<List<ProgramBean>>() {
                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onSuccess(List<ProgramBean> programBeans) {

                    }
                });
        
    }

    /**
     * 获取节目详情
     *
     * @param proId
     */
    @Override
    public void getDetail(String proId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url =LiveUrl.GET_PROGRAM_DETAIL + "?timestamp=" + time;
        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "proId=" + proId))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<ProgramDetailBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setDetailError(1, "暂无节目！");
                        } else {
                            mPresenter.setDetailError(2, "解析异常，加载失败！");
                        }
                    }

                    @Override
                    public void onSuccess(ProgramDetailBean programDetailBean) {
                        if (programDetailBean != null && programDetailBean.getProgram() != null) {
                            mPresenter.setDetail(programDetailBean);
                        } else {
                            mPresenter.setDetailError(1, "暂无节目！");
                        }
                    }
                });
    }

    /**
     * 获取回放地址
     *
     * @param name
     * @param startTime
     * @param endTime
     */
    @Override
    public void getPlayPath(String name, String startTime, String endTime) {

        EasyHttp.get(LiveUrl.GET_PROGRAM_PLAYPATH)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("streamName", name)
                .params("startTime", startTime)
                .params("endTime", endTime)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getPathError("网络异常，获取失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            if (StringUtils.isEmpty(apiResult.getData().toString())) {
                                mPresenter.getPathError("节目单错误，播放失败！");
                            } else {
                                mPresenter.getPathSuccess(apiResult.getData().toString());
                            }
                        } else {
                            mPresenter.getPathError("节目单错误，播放失败！");
                        }
                    }
                });
    }

    @Override
    public void click(String proId) {

        EasyHttp.get(LiveUrl.GET_PROGRAM_CLICK)
                .params("proId", proId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onSuccess(String s) {

                    }
                });

    }
}
